package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        keyEventDemo();
        touchEventDemo();
    }

    private void touchEventDemo() {
        setContentView(R.layout.touch_main);
        TextView touchView = (TextView) findViewById(R.id.touchView);
        TextView historyView = (TextView) findViewById(R.id.historyView);
        TextView detailView = (TextView) findViewById(R.id.detailView);
        touchView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int action = motionEvent.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        display("ACTION_DOWN", motionEvent, detailView);
                        break;
                    case MotionEvent.ACTION_UP:
                        int historySize = processHistory(motionEvent);
                        historyView.setText("历史数据量：" + historySize);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        display("ACTION_MOVE", motionEvent, detailView);
                        break;
                }
                return false;
            }
        });
    }

    private void display(String eventType, MotionEvent event, TextView view) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        float pressure = event.getPressure();
        float size = event.getSize();
        int RawX = (int) event.getRawX();
        int RawY = (int) event.getRawY();

        String msg = "";
        msg += "事件类型：" + eventType + "\n";
        msg += "相对坐标：" + String.valueOf(x) + "," + String.valueOf(y) + "\n";
        msg += "绝对坐标：" + String.valueOf(RawX) + "," + String.valueOf(RawY) + "\n";
        msg += "触点压力：" + String.valueOf(pressure) + "，  ";
        msg += "触点尺寸：" + String.valueOf(size) + "\n";
        view.setText(msg);
    }

    private int processHistory(MotionEvent event) {
        int historySize = event.getHistorySize();
        for (int i = 0; i < historySize; i++) {
            long time = event.getHistoricalEventTime(i);
            float pressure = event.getHistoricalPressure(i);
            float x = event.getHistoricalX(i);
            float y = event.getHistoricalY(i);
            float size = event.getHistoricalSize(i);
            // 处理过程......
        }
        return historySize;
    }


    private void keyEventDemo() {
        setContentView(R.layout.activity_main);
        EditText editText = (EditText) findViewById(R.id.editText);
        TextView textView = (TextView) findViewById(R.id.textView);
        CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
        editText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                int unicodeChar = keyEvent.getUnicodeChar();
                int metaState = keyEvent.getMetaState();
                String msg = "";
                msg += "按键动作:" + keyEvent.getAction() + "\n";
                msg += "按键代码:" + keyCode + "\n";
                msg += "按键字符:" + (char) unicodeChar + "\n";
                msg += "UNICODE:" + unicodeChar + "\n";
                msg += "重复次数:" + keyEvent.getRepeatCount() + "\n";
                msg += "功能键状态:" + metaState + "\n";
                msg += "硬件编码:" + keyEvent.getScanCode() + "\n";
                msg += "按键标志:" + keyEvent.getFlags() + "\n";
                textView.setText(msg);
                //返回true，阻止事件传递
                //返回false，允许继续传递按键事件
                return checkBox.isChecked();
            }
        });
    }
}